package com.app.groovy;

import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import org.codehaus.groovy.runtime.InvokerHelper;

import java.util.HashMap;

/**
 * groovy
 *
 * @author ch
 * @date 2022/12/15 17:47
 */
public class GroovyFactory {

    public static Script createScript1(String code) {
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
        Binding binding = new Binding(new HashMap(16));
        return InvokerHelper.createScript(groovyClassLoader.parseClass(code), binding);
    }

    public static Script createScript2(String code) {
        GroovyShell groovyShell = new GroovyShell();
        return groovyShell.parse(code);
    }

}
